package com.ermace.robo.jumper;

import com.ermace.robo.jumper.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

public class HighScores extends Activity {
	
	private TableLayout highScoreLayout;
	private SharedPreferences highScores;
	private SharedPreferences parameters;
	private SharedPreferences highscoresNames;
	
	private boolean backPressed = false;

	public static String[] sortHighscores(String[] List, SharedPreferences highscores)
	{
		String[] sorted = new String[List.length];
		boolean finished = false;
		int maxvalue = 0;
		int a = 0;
		while(a < List.length){
			for(int i = 0; i < List.length; i++)
			{
				if(highscores.getInt(List[maxvalue], 0) < highscores.getInt(List[i], 0) )
					maxvalue = i;
			}
			sorted[a] = List[maxvalue];
			List[maxvalue] = null;
			a++;
		}
		return sorted;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscores);
        
       
        highScores =  getSharedPreferences("highScores", MODE_PRIVATE);   
        highscoresNames =  getSharedPreferences("highscoresNames", MODE_PRIVATE);
        parameters =  getSharedPreferences("parameters", MODE_PRIVATE);
        
        highScoreLayout = (TableLayout) findViewById(R.id.highScoreLayout);
        refreshScores(null);
    }

    private void refreshScores(String newHighScore) {
    	
    	String[] highScoresList = highScores.getAll().keySet().toArray(new String[0]);
    	highScoresList = sortHighscores(highScoresList, highScores);
		for(int index = 0; index < highScoresList.length; ++index)
			makeScoreGui(highscoresNames.getString(highScoresList[index], "no Name"), "" + highScores.getInt(highScoresList[index], 0));
	}

	private void makeScoreGui(String name, String score) {
		
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	View newMapView = inflater.inflate(R.layout.new_highscore_view, null);
    	
    	TextView playerName = (TextView) newMapView.findViewById(R.id.playerName);
    	playerName.setText(name);
    	
    	TextView mapPreview = (TextView) newMapView.findViewById(R.id.highScore);
    	mapPreview.setText("" + score);
    	
    	highScoreLayout.addView(newMapView);
	}
	
	private void clearHighscores()
	{
		SharedPreferences.Editor ed = highScores.edit();
		SharedPreferences.Editor ed2 = highscoresNames.edit();
		ed.clear();
		ed.commit();
		ed2.clear();
		ed2.commit();
		
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.highscores, menu);
        return true;
    } 
	
	@Override
	protected void onResume() {
		super.onResume();
			MainActivity.resumeMusic();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
			if(!backPressed)
				MainActivity.pauseMusic();
	}
	
	@Override
	public void onBackPressed() {
		backPressed = true;
		super.onBackPressed();
	}
}
